<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    //显示首页
    public function index(){
        
        //获取商品分类 
        $category=M('category');
        $catelist=$category->where('pid=0')->select();            
        foreach($catelist as $k=>$v)//对一级分类循环
        {            
            $catelist[$k]['child']=fenlei($v['cid']);  //调用fenlei(),取这个ID的子分类            
        }
        $this->assign("catelist",$catelist);
        
        //获取推荐的商品，两个条件  回收站no  推荐标志为yes
        $best=M('goods');        
        $bestlist=$best->where("is_best='yes'  and recycle='no'")->limit(4)->select();
        $this->assign("bestlist",$bestlist);
        //显示视图
        $this->display();
    }
    
    //显示商品的详细信息
    public function goods(){
        $gid=I("get.gid",0);
        $goods=M(goods);
        $goodsrow=$goods->where("gid=$gid")->find();//获取一条  
        
        //获得商品的1、2、3级分类
        $cid=$goodsrow['cid'];
        $types=D("Category")->getPidList($cid);
        
        //获得商品的属性
        $attr=D("GoodsAttr")->getData($cid,$gid);
        
        $this->assign("goodsrow",$goodsrow);
        $this->assign("types",$types);
        $this->assign("attr",$attr);
        $this->display();
    }
    
    
    public function find(){
        //获取参数
        $cid=I('get.cid',0);
        //查询cid的分类名称
        $cname=M('category')->where("cid=$cid")->getField('cname');
        
        $gdata=D('Goods')->getByFilter(
                'gid,gname,price,thumb',
                array('cid'=>$cid)
                );
        $this->assign('gdata',$gdata);
        $this->assign('cname',$cname);
        $this->display();
    }
}